package org.tio.sitexxx.service.service.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.conf.Conf;
import org.tio.sitexxx.service.model.conf.ConfDev;

import cn.hutool.core.util.StrUtil;

/**
 * 配置信息加载
 * @author tanyaowu
 *
 */
public class ConfService {

	private static Logger log = LoggerFactory.getLogger(ConfService.class);

	public static final ConfService me = new ConfService();

	final static Conf confDao = new Conf().dao();

	final static ConfDev confDevDao = new ConfDev().dao();

	/**
	 * key: name, value: value
	 */
	private static Map<String, String> cacheData = null;

	/**
	 * 
	 */
	public static void clearCache() {
		synchronized (ConfService.class) {
			cacheData = null;
		}
	}

	private static void loadData() {
		//		clearCache();

		Map<String, String> tempCache = new HashMap<>();
		List<Conf> list = null;
		try {
			list = confDao.find("select * from conf");
			if (list == null) {
				list = new ArrayList<>();
			}
			List<ConfDev> list2 = confDevDao.find("select * from conf_dev");

			if (list2 != null) {
				for (ConfDev item : list2) {
					Conf conf = new Conf();
					conf.setName(item.getName());
					conf.setValue(item.getValue());
					conf.setRemark(item.getRemark());
					list.add(conf);
				}
			}

			if (list.size() == 0) {
				return;
			}

			for (Conf conf : list) {
				tempCache.put(conf.getName(), conf.getValue());
			}

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			cacheData = tempCache;
		}
	}

	/**
	 * 根据参数名字获取参数值
	 * @param name
	 * @param defaultValue 默认值
	 * @return
	 */
	public static String getString(String name, String defaultValue) {
		if (cacheData == null) {
			synchronized (ConfService.class) {
				if (cacheData == null) {
					loadData();
				}
			}
		}
		String value = cacheData.get(name);
		if (!StrUtil.isBlank(value)) {
			return value;
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Integer getInt(String name, Integer defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Integer.parseInt(value);
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Byte getByte(String name, Byte defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Byte.parseByte(value);
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 * 
	 */
	public static Float getFloat(String name, Float defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Float.parseFloat(value);
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 * 
	 */
	public static Double getDouble(String name, Double defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Double.parseDouble(value);
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @param trueValue
	 * @return
	 */
	public static Boolean getBoolean(String name, String defaultValue, String trueValue) {
		String value = getString(name, defaultValue);
		return value.equalsIgnoreCase(trueValue);
	}

	/**
	 * 值为true、TRUE的返回true
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Boolean getBoolean(String name, String defaultValue) {
		String value = getString(name, defaultValue);
		return value.equalsIgnoreCase("true");
	}
}
